package com.five.taotao.dao;

import android.content.Context;

import com.five.taotao.db.MyOrmLiteSqliteOpenHelper;
import com.five.taotao.entity.Product;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2016/11/8.
 */
public class ProductListBeanDao {

    //OrmLiteSqlite 封装的数据库操作对象
    private Dao<Product.ListBean, Integer> dao;

    /**
     * @param context
     */
    public ProductListBeanDao(Context context) {
        MyOrmLiteSqliteOpenHelper dataBaseHelper = MyOrmLiteSqliteOpenHelper.getInstance(context);//获得DataBaseHelper实例
        try {
            dao = dataBaseHelper.getDao(Product.ListBean.class);//getDao()方法获得userdao对象
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过dao对象向数据库写入数据
     *
     * @param
     * @return
     */
    public long addProduct(Product.ListBean song) {
        int id = 0;
        try {
            id = dao.create(song);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return id;
    }

    /**
     * 批量写入
     *
     * @param
     */
    public void addProductList(List<Product.ListBean> songlist) {
        for (Product.ListBean f : songlist) {
            addProduct(f);
        }
    }


    /**
     * 通过dao对象查找所有数据库数据
     *
     * @return
     */
    public List<Product.ListBean> findAllProduct() {
        List<Product.ListBean> tmp = new ArrayList<>();
        try {
            List<Product.ListBean> tt = dao.queryForAll();
            int num = tt.size();
            for (int i = 0; i < num; i++) {
                tmp.add(tt.get(num-i-1));
            }
            return tmp;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 通过dao对象查找所有数据库数据
     *
     * @return
     */
    public List<Product.ListBean> findAllProductOrder() {
        try {
            List<Product.ListBean> tt = dao.queryForAll();
            return tt;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查找指定条件的数据库
     *
     * @return
     */
    public boolean findProduct(String product_id) {
        boolean flag = false;
        List<Product.ListBean> is = new ArrayList<>();

        QueryBuilder<Product.ListBean, Integer> builder = dao.queryBuilder();
        try {
            is = builder.where()
                    .eq("productId", product_id).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        if (is.size() == 0) {
            flag = false;
        } else {
            flag = true;
        }
        return flag;
    }


    /**
     * 查找所有收藏的商品
     *
     * @return
     */
    public List<Product.ListBean> findProductByCollected(int collected) {
        List<Product.ListBean> is = new ArrayList<>();
        QueryBuilder<Product.ListBean, Integer> builder = dao.queryBuilder();
        try {
            is = builder.where()
                    .eq("collected", collected).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return is;
    }


    /**
     * 删除所有音乐
     */
    public void deleteAllProduct() {
        try {
            DeleteBuilder<Product.ListBean, Integer> builder = dao.deleteBuilder();
            builder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除指定的数据元素
     */
    public void deleteProduct(String song_id) {
        DeleteBuilder<Product.ListBean, Integer> builder = dao.deleteBuilder();
        try {
            builder.where()
                    .eq("song_id", song_id);
            builder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

